﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AEM.Data.Enum;
using System.Runtime.Remoting.Messaging;
using System.Data;
using AEM.Utils;
using System.IO;

namespace AEM.Data.BLL.Actions
{
    public static class HallCallLog
    {
        static HallCallLog()
        {
            Add = doAdd;
        }

        private static bool doAdd(SerialDataEventArgs args)
        {
            try
            {
                string dir = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "datalogs", DateTime.Now.ToString("yyyyMMdd"));
                if (!Directory.Exists(dir))
                    Directory.CreateDirectory(dir);
                string filename = Path.Combine(dir, args.LiftNum.ToString() + ".xml");

                Data.DataLog logdb = new Data.DataLog(filename);

                IEnumerable<Data.DataLog.TableHallCallLogRow> result;

                result = (from Data.DataLog.TableHallCallLogRow r in logdb.TableHallCallLog.Rows
                          where r.StartTime == r.EndTime
                          select r);

                foreach (Data.DataLog.TableHallCallLogRow r in result)
                {
                    if (r.CallType == (int)CallType.Up)
                    {
                        IEnumerable<int> squares = args.Data.OuterUpRing.Select(x => x = r.Floor);
                        if (squares.Count() > 0) args.Data.OuterUpRing.Remove(squares.ToArray()[0]);
                        else r.EndTime = args.TimeRecieved;
                    }
                    else if (r.CallType == (int)CallType.Down)
                    {
                        IEnumerable<int> squares = args.Data.OuterDownRing.Select(x => x = r.Floor);
                        if (squares.Count() > 0) args.Data.OuterDownRing.Remove(squares.ToArray()[0]);
                        else r.EndTime = args.TimeRecieved;
                    }
                }
                if (args.Data.OuterUpRing != null)
                {
                    foreach (int i in args.Data.OuterUpRing)
                    {
                        Data.DataLog.TableHallCallLogRow r = logdb.TableHallCallLog.NewTableHallCallLogRow();
                        r.LiftNum = args.LiftNum;
                        r.Floor = i;
                        r.CallType = (int)CallType.Up;
                        r.StartTime = r.EndTime = args.TimeRecieved;
                        logdb.TableHallCallLog.AddTableHallCallLogRow(r);
                    }

                }
                if (args.Data.OuterDownRing != null)
                {
                    foreach (int i in args.Data.OuterDownRing)
                    {
                        Data.DataLog.TableHallCallLogRow r = logdb.TableHallCallLog.NewTableHallCallLogRow();
                        r.LiftNum = args.LiftNum;
                        r.Floor = i;
                        r.CallType = (int)CallType.Down;
                        r.StartTime = r.EndTime = args.TimeRecieved;
                        logdb.TableHallCallLog.AddTableHallCallLogRow(r);
                    }
                }


                logdb.TableHallCallLog.AcceptChanges();
                logdb.Save();
            }
            catch
            {
            }
            return false;
        }

        public static void AddCallback(IAsyncResult tag)
        {
            AsyncResult result = (AsyncResult)tag;
            Func<SerialDataEventArgs, bool> del = (Func<SerialDataEventArgs, bool>)result.AsyncDelegate;
            del.EndInvoke(tag);
        }

        /// <summary>
        /// 添加上下呼记录记录
        /// </summary>
        public static Func<SerialDataEventArgs, bool> Add;
    }
}
